<?php
function toCSV($file_name, array $data, array $colHeaders = array(), $asString = false) {
    $fp = $asString
        ? fopen($file_name.".csv", "w+")
        : fopen($file_name.".csv", "w");

    fwrite($fp,"\xEF\xBB\xBF");
    if (!empty($colHeaders)) {
        fputcsv($fp, $colHeaders);
    }

    foreach ($data as $record) {
        fputcsv($fp, $record);
    }

    if ($asString) {
        rewind($fp);
        $returnVal = stream_get_contents($fp);
        fclose($fp);
        return $returnVal;
    }
    else {
        fclose($fp);
    }
}
function getHeaders(){
    $headers[] = "题型";
    $headers[] = "子题型";
    $headers[] = "题干";
    $headers[] = "选项";
    $headers[] = "答案";
    $headers[] = "解析";
    $headers[] = "难度";
    $headers[] = "分数";
    $headers[] = "课程结构";
    return $headers;
}
//
//// for test
//$data[] = ["a1","a2"];
//$data[] = ["b1","b2"];
//
//$file_name = "电工与电子技术";
//toCSV($file_name,$data,getHeaders(),true);